<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="http://172.17.149.196:8080/jquery/jquery-2.1.3.min.js"></script>
    <script src="lib/comet4j.js"></script>
    <script type="text/javascript">
        function init() {
            JS.Engine.on({
                //侦听一个channel
                hello: function (msg) {
                    console.info('receive msg:' + msg);
                    document.getElementById("chatlog").textContent += msg + "\n";
                }
            });
            JS.Engine.on('start', function (cId, channelList, engine) {
                console.info('连接已建立，连接ID为：' + cId);
            });
            JS.Engine.on('stop', function (cause, cId, url, engine) {
                console.info('连接已断开，连接ID为：' + cId + ',断开原因：' + cause + ',断开的连接地址：' + url);
            });

            JS.Engine.start('conn');
        }

        function postToServer() {
            $.ajax({
                url: 'cometecho?msg=' + document.getElementById("msg").value,
                async: true
            });
        }

        function closeConnect() {
            JS.Engine.stop('conn');
        }
    </script>
</head>
<body onload="init()">
<textarea id="chatlog" readonly rows="10" cols="50"></textarea><br/>
<input id="msg" type="text"/>
<button onClick="postToServer()">Send</button>
<button onClick="closeConnect()">End</button>
</body>
</html>